package com.lanou.lenanapp.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;


import com.lanou.lenanapp.R;
import com.lanou.lenanapp.adapter.AfficheViewPagerAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 公告牌界面
 */
public class AfficheActivity extends Activity implements View.OnClickListener,ViewPager.OnPageChangeListener {

    private ImageView back_affiche;
    private ViewPager viewPager;
    private AfficheViewPagerAdapter pagerAdapter;
    private ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    //图片的数组
    private int []image = {R.mipmap.image_affiche0, R.mipmap.image_affiche1, R.mipmap.image_affiche2,
                            R.mipmap.image_affiche3};

    private ImageView[] image_small = new ImageView[4];

    //文字的数组
    private String [] text = {"广大狼友们请注意:" + "\n     软件将在今晚十一点更新,给狼友们带来的不便请谅解.",
            "狼友们请注意:" + "\n     11.11日内涵福利社的新浪微博官方账号,狼友们均有机会被抽中幸运大奖...女朋友一位." + "还等什么?...快去关注吧!",
            "狼友们请注意:" + "\n     庆祝内涵福利社成立三周年,感谢广大狼友们给小编的动力,小编将在每晚的十一点整给大家发放福利",
            "狼友们请注意:" + "\n     由于版本更新..android2.0版本以前手机可能无法播放视频了,我们会尽快解决,请狼友们耐心等待"};
    //延时跳转对象
    private Handler handler;

    private LayoutInflater inflater;
    private int maxItem = 0;

    private Runnable r = new Runnable() {
        @Override
        public void run() {
            int index = viewPager.getCurrentItem();
            viewPager.setCurrentItem(++index);
            //延时两秒改变下标
            handler.postDelayed(r, 4000);
        }
    };
    private LinearLayout v;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_affiche);

        init();

        //遍历数组
        for (int i = 0; i < image.length; i ++){
            Map<String, Object> map = new HashMap<>();
            map.put("image", image[i]);
            map.put("text", text[i]);
            list.add(map);
        }
        maxItem = list.size();//设置最大长度
        handler = new Handler();//初始化适配器
        handler.postDelayed(r, 4000);//延时改变下标
        pagerAdapter.addData(list);//添加到适配器

        initIndicator();
        viewPager.addOnPageChangeListener(this);
    }

    private void init() {
        back_affiche = (ImageView) findViewById(R.id.back_affiche);
        back_affiche.setOnClickListener(this);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        pagerAdapter = new AfficheViewPagerAdapter();
        viewPager.setAdapter(pagerAdapter);
    }

    /**
     * 初始化引导图标
     * 创建多个小圆点 组装到线型布局里
     * **/
    private void initIndicator() {
        ImageView imageView;
        //线性水平布局 负责动态调整导航图标
        v = (LinearLayout) findViewById(R.id.indicator);
        //创建小圆点
        for (int i = 0; i < image.length; i ++ ) {
            imageView = new ImageView(this);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
            layoutParams.setMargins(20, 30, 20, 30);
            imageView.setLayoutParams(layoutParams);
            image_small [i] = imageView;

            if (i == 0){//初始化 第一个为选中状态
                image_small[i].setBackgroundResource(R.drawable.iconfont_yuandian_2);
            }else{
                image_small[i].setBackgroundResource(R.drawable.iconfont_yuandian_3);
            }
            v.addView(image_small[i]);//将小圆点数组 添加到线性布局中
        }

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back_affiche:
                finish();
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        //遍历数组 设置小圆点的图片
        for (int i = 0; i < image_small.length; i ++){
            image_small[(position%list.size())].setBackgroundResource(R.drawable.iconfont_yuandian_2);
            //判断 如果当前位置不等于i 则设置成另一个图片
            if ((position%list.size()) != i){
                image_small[i].setBackgroundResource(R.drawable.iconfont_yuandian_3);
            }
        }



    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
